//
//  LeftViewController.m
//  Blogs
//
//  Created by cnfol on 14-2-24.
//  Copyright (c) 2014年 cnfol. All rights reserved.
//

#import "LeftViewController.h"
#import "DDMenuController.h"
#import "UserInfoViewController.h"
#import "LeftViewCell.h"




@interface LeftViewController ()<UIActionSheetDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSArray *titleArray;  //标题
@property(nonatomic,strong)NSArray *Images; //图片
@property(nonatomic,strong)UIButton *onOff; //开关
@property(nonatomic,strong)UIImage *btnImage; //选中按钮
@property(nonatomic,strong)NSUserDefaults *userDefault;

@end

@implementation LeftViewController
/*返回一个新的初始化视图控制器在指定束nib文件。
 与视图控制器关联的nib文件的名称。nib文件名不能包含任何领先的路径信息。如果指定的零，nibname属性设置为零。 */
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}


- (void)accountHaveExit
{
    /**/
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:1 inSection:0];
    [self.tableV reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)colorModeChange
{
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:2 inSection:0];
    [self.tableV reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationNone];

}

- (void)accountHaveLogin
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //不知道是什么
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountHaveExit) name:@"AccountExit" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountHaveExit) name:HaveLoginNotification object:nil];
    
    
    
    
    
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor clearColor];
    self.tableV.backgroundView = nil;
    if (iOS7) {
        self.tableV.separatorInset = UIEdgeInsetsMake(0, 50, 0, 0);
    }
    self.tableV.scrollEnabled = NO;
    self.tableV.rowHeight = 50;
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    
    self.titleArray = [NSArray arrayWithObjects:@"电影",@"夜间模式",@"影院",@"我的",@"设置", nil];
    NSLog(@"%@",self.title);
    
    self.userDefault = [NSUserDefaults standardUserDefaults];
    BOOL number = [self.userDefault boolForKey:@"switchNumber"];
    UIColor *color;
    if (number) {
        self.onOff.selected = YES;
        color = NIGHT_COLOR;
        self.btnImage = [UIImage imageNamed:@"YOpen"];
    
    }else{
        self.onOff.selected = NO;
        color = [UIColor whiteColor];
        self.btnImage = [UIImage imageNamed:@"Close"];
    }
    [self.onOff setBackgroundImage:self.btnImage forState:UIControlStateNormal];
    self.view.backgroundColor = color;
    [self changeImage];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
   
}

#pragma mark - 表格视图数据源代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"title.count = %lu",(unsigned long)self.titleArray.count);
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ListViewCellId = @"ListViewCellId";
    
    LeftViewCell *cell = [[LeftViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ListViewCellId];
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [AppTools colorWithHexString:@"#ebeff0"];

    tableView.separatorColor = UIColorFromRGB(0xAAAAAA);
    cell.nameLabel.text = self.titleArray[indexPath.row];
    
    BOOL number = [NSUSER_DEFAULTS boolForKey:@"switchNumber"];
    if (number) {
        cell.nameLabel.textColor = UIColorFromRGB(0xd4d4d4);
    }else{
        cell.nameLabel.textColor  = [UIColor blackColor];
    }

    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DDMenuController *menuController = (DDMenuController*)((AppDelegate*)[[UIApplication sharedApplication] delegate]).menuController;

    switch (indexPath.row) {
        case 0:
            [menuController showUserinfoController:YES];

            break;
        case 1:
        {
            
            self.userDefault = [NSUserDefaults standardUserDefaults];
            BOOL number = [self.userDefault boolForKey:@"switchNumber"];
            number = !number;
            if (number) {
                self.titleArray = [NSArray arrayWithObjects:@"电影",@"日间模式",@"影院",@"我的",@"设置", nil];
                [self colorModeChange];
            }else{
                self.titleArray = [NSArray arrayWithObjects:@"电影",@"夜间模式",@"影院",@"我的",@"设置", nil];
                [self colorModeChange];
            }
            [NSUSER_DEFAULTS setBool:number forKey:@"switchNumber"];
            [NSUSER_DEFAULTS synchronize];
            NSLog(@"mumber == %d",number);
            NSNumber *valueNumber = [NSNumber numberWithBool:number];
            [[NSNotificationCenter defaultCenter] postNotificationName:NightNotification object:valueNumber];
            UIColor *color;
            if (number) {
                
                self.onOff.selected = YES;
                color = NIGHT_COLOR;
                self.btnImage = [UIImage imageNamed:@"YOpen"];
                
            }else{
                self.onOff.selected = NO;
                color = [UIColor whiteColor];
                self.btnImage = [UIImage imageNamed:@"Close"];
            }
            [self.onOff setBackgroundImage:self.btnImage forState:UIControlStateNormal];
            self.view.backgroundColor = color;
            [self changeImage];
        }
            break;
        case 2:
            [menuController showWordSizeController:YES];
            
            break;
        case 3:
            [menuController showSuggestController:YES];
            
            break;
        case 4:
            [menuController showAboutController:YES];
            break;
        
    }
    
}

-(void)tableView:(UITableView*)tableView  willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath

{
    
    [cell setBackgroundColor:[UIColor clearColor]];
    
}

-(void)changeImage{
    
    if ([NSUSER_DEFAULTS boolForKey:@"switchNumber"]) {
        self.Images = @[@"y_MenuHome.png",@"y_MenuUser.png",@"y_MenuDay.png",@"y_MenuFont.png",@"y_MenuEmail.png",@"y_MenuAbout.png"];
    
    }else{
        self.Images = @[@"MenuHome.png",@"MenuUser.png",@"MenuNight.png",@"MenuFont.png",@"MenuEmail.png",@"MenuAbout.png"];
    }
    
    [self.tableV reloadData];
}

-(void)switchAction:(UIButton *)sw
{
    sw.selected = !sw.selected;
    UIColor *color;
    if (sw.selected) {
       
        [sw setBackgroundImage:[UIImage imageNamed:@"YOpen"]forState:UIControlStateNormal];
        color = NIGHT_COLOR;
    }else{
       
        [sw setBackgroundImage:[UIImage imageNamed:@"Close"]forState:UIControlStateNormal];
        color = [UIColor whiteColor];
    }
    self.view.backgroundColor = color;
    if (sw.selected) {
        [self.userDefault setBool:YES forKey:@"switchNumber"];
    }
    else{
        [self.userDefault setBool:NO forKey:@"switchNumber"];
    }

    [self.userDefault synchronize];
    [self changeImage];
    //发送模式改变通知
    NSNumber *number = [NSNumber numberWithBool:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:NightNotification object:number];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

@end
